package com.dk.service.Impl;

import com.dk.mapper.TabCollectMapper;
import com.dk.mapper.TabRouteMapper;
import com.dk.pojo.TabCollect;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabUser;
import com.dk.service.CollectService;
import com.dk.utils.PageBean;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    TabCollectMapper tabCollectMapper;
    @Autowired
    TabRouteMapper tabRouteMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public PageBean<TabRoute> getInfo(Integer pageNum, Integer pageSize, TabUser userLogin) {
        PageBean<TabRoute> pageBean = new PageBean<>();
        Integer startIndex = (pageNum - 1) * pageSize;
        Integer totals = pageNum*pageSize;
        Integer[] ridContent = tabCollectMapper.selectCollectByUidAndPage(startIndex,totals,userLogin.getUid());
        //List<Integer> ridList = tabCollectMapper.selectCollectByUid(userLogin.getUid());
        //PageHelper.startPage(pageNum,pageSize);
        //获取总条数
        Integer totalCount = tabCollectMapper.getTotals(userLogin.getUid());

        //创建存放对应用户收藏的路线集合
        List<TabRoute> routes = new ArrayList<>();
        //根据uid查找收藏的路线ridList
        for (Integer r : ridContent) {
            TabRoute tabRoute = tabRouteMapper.selectByPrimaryKey(r);
            routes.add(tabRoute);
        }
        /*for (Integer rid : ridList) {
            TabRoute tabRoute = tabRouteMapper.selectByPrimaryKey(rid);
            routes.add(tabRoute);
        }*/
        //PageInfo<TabRoute> pageInfo = new PageInfo<>(routes);

        pageBean.setResult(routes);
        pageBean.setPageNumber(pageNum);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public List<TabRoute> getInfoLimitFour() {
        String key = "RouteForSet:";
        Gson gson = new Gson();
        ZSetOperations opsForZSet = redisTemplate.opsForZSet();
        if (redisTemplate.hasKey(key)) {
            //查询总条数
            List<TabRoute> list = new ArrayList<>();
            //获取set集合
            Set<String> setForRoute = opsForZSet.reverseRange(key, 0, 3);
            //循环遍历set 使用gson将字符串转为对象再放入到list集合中
            for (String tabRoute : setForRoute) {
                list.add(gson.fromJson(tabRoute, TabRoute.class));
            }
            return list;
        }else {
            //第一次访问从数据库查所有数据并存入redis
            List<TabRoute> routes = tabRouteMapper.getInfoRank();
            for (TabRoute route : routes) {
                String s = gson.toJson(route);
                //循环遍历插入zSet集合
                opsForZSet.add(key,s,route.getCount());
            }
            List<TabRoute> list = new ArrayList<>();
            //获取set集合
            Set<String> setForRoute = opsForZSet.reverseRange(key, 0, 3);
            for (String tabRoute : setForRoute) {
                list.add(gson.fromJson(tabRoute, TabRoute.class));
            }
           return list;
        }
    }

    @Override
    public List<Integer> selectCollectByUid(Integer uid) {
        return tabCollectMapper.selectCollectByUid(uid);
    }

    @Override
    public int deleteCancelCollect(Integer rid, Integer uid) {
        TabCollect tabCollect = tabCollectMapper.selectCollectByUidAndUid(rid,uid);
        int i = tabCollectMapper.deleteByPrimaryKey(tabCollect.getUrid());
        return i;
    }
}
